package com.shanks.home.net;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Logger;

import com.shanks.home.model.SocketCommand;

/**
 *The Coalaa Inc 
 *Copyright (c) 2013, Coalaa Inc. All rights reserved. 
 * @author The Mobile Dev Team, Coalaa Inc.
 *          Viking<viking@coalaa.com>
 * 2013-6-21
 */
public class SocketCommandBody {
	
	public final static int MAX_ROOM_NUM = 50;
	
	
	/*************************解包函数***********************
	 * 函数头  ：ByteHashMap decode(short code, byte[] buf)
	 * 入口参数：
	 * 			1.short code			：命令字
	 * 			2.byte[] buf			：数据缓冲
	 * 
	 * 返回参数：
	 * 			byte[]					：组包数据 
	 *******************************************************/		
	public static SocketByteHashMap decode(short code, byte[] buf){
		
		Logger.getLogger("").info("shanks:code:" + code);
		
		SocketByteHashMap result = null;
		@SuppressWarnings("unused")
		String desc = null;
		try{
			switch (code) {
			case SocketCommand.SVR_CMD_LOGIN_SUCC:
				result = decodeServerEntryRoomSuccess(buf);
				desc = "登录成功";
				break;
			// 退出房间成功
			case SocketCommand.SVR_CMD_LOGOUT_SUCC:
				desc = "退出成功";
				break;
			//重连登陆成功
			case SocketCommand.SVR_CMD_RELOGIN_SUCC:
				desc = "重连登陆成功";
				break;
			//登陆失败
			case SocketCommand.SVR_CMD_LOGIN_FAIL:
				desc = "登陆失败";
				break;
			//坐下失败
			case SocketCommand.SVR_CMD_SITEDOWN_FAIL:
				desc = "坐下失败";
				break;
			//广播：游戏开始
			case SocketCommand.SVR_BCCMD_GAME_START:
				desc = "游戏开始";
				break;
			//广播：翻出FLOP牌
			case SocketCommand.SVR_BCCMD_CARD_FLOP:
				result = decodeServerPublicCards(buf, Enum_Server_PublicCards.flop);
				desc = "FLOP";
				break;
			//广播：翻出TRUN牌
			case SocketCommand.SVR_BCCMD_CARD_TRUN:
				result = decodeServerPublicCards(buf, Enum_Server_PublicCards.turn);
				desc = "TRUN";
				break;
			//广播：翻出RIVER牌
			case SocketCommand.SVR_BCCMD_CARD_RIVER:
				result = decodeServerPublicCards(buf, Enum_Server_PublicCards.river);
				desc = "RIVER";
				break;
			//广播：下注成功
			case SocketCommand.SVR_BCCMD_BET_SUCC:
				result = decodeServerOperationResult(buf);
				desc = "下注成功";
				break;
			//广播：轮到某个用户下注
			case SocketCommand.SVR_BCCMD_BET_TURN_TO:
				result = decodeServerOperationTurns(buf);
				desc = "轮到某个用户下注";
				break;
			//广播：正常牌局游戏结束
			case SocketCommand.SVR_BCCMD_GAME_OVER:
				desc = "正常牌局游戏结束";
				break;
			//广播：用户坐下
			case SocketCommand.SVR_BCCMD_SITDOWN:
				desc = "用户坐下";
				break;
			//广播：用户站起
			case SocketCommand.SVR_BCCMD_STAND:
				desc = "用户站起";
				break;
			//广播：奖池
			case SocketCommand.SVR_BCCMD_CHIPSPOTS:
				result = decodeServerOperationPots(buf);
				desc = "奖池";
				break;
			//广播：聊天
			case SocketCommand.SVR_BCCMD_CHATMSG:
				result = decodeServerChat(buf);
				desc = "聊天";
				break;
			//广播：表情
			case SocketCommand.SVR_BCCMD_EMOTION:
				result = decodeServerChatEmotion(buf);
				desc = "表情";
				break;
			//广播：All-In导致游戏结束标识
			case SocketCommand.SVR_BCCMD_ALLIN:
				desc = "All-In导致游戏结束标识";
				break;
			//广播：SVR广播亮出手牌
			case SocketCommand.SVR_BCCMD_SHOW_HAND_CARD:
				desc = "SVR广播亮出手牌";
				break;
			//广播：赠送礼物
			case SocketCommand.SVR_BCCMD_SEND_GIFT:
				desc = "赠送礼物";
				break;
			//广播：赠送筹码
			case SocketCommand.SVR_BCCMD_SEND_CHIPS:
				desc = "赠送筹码";
				break;
			//通知：赠送筹码失败
			case SocketCommand.SVR_CMD_SEND_CHIPS_FAIL:
				desc = "赠送筹码失败";
				break;
			//通知：破产
			case SocketCommand.SVR_CMD_USER_CRASH:
				desc = "破产";
				break;
			//广播：互动道具
			case SocketCommand.SVR_BCCMD_SEND_HD:
				result = decodeServerInteractiveProps(buf);
				desc = "互动道具";
				break;
			//广播：踢人
			case SocketCommand.SVR_BCCMD_KITOUT:
				desc = "踢人";
				break;
			//广播：加为好友
			case SocketCommand.SVR_BCCMD_ADD_FRIEND:
				result = decodeServerAddFriend(buf);
				desc = "加为好友";
				break;
			//通知：SVR升级成功
			case SocketCommand.SVR_BCCMD_SVR_SWITCH:
				desc = "SVR升级成功";
				break;
			//通知：停服通知
			case SocketCommand.SVR_BCCMD_SVR_STOP:
				desc = "停服通知";
				break;
			
			/************ 淘汰赛 ************/
			//广播比赛开始
			case SocketCommand.SVR_BCCMD_MATCH_START_K:
				desc = "广播比赛开始";
				break;
			//广播比赛结束
			case SocketCommand.SVR_BCCMD_MATCH_END_K:
				desc = "广播比赛结束";
				break;
			//广播比赛关闭
			case SocketCommand.SVR_BCCMD_MATCH_CLOSE_K:
				desc = "广播比赛关闭";
				break;
			//广播比赛桌子id
			case SocketCommand.SVR_BCCMD_TABLE_ID:
				desc = "广播比赛桌子id";
				break;
			//广播所有名次
			case SocketCommand.SVR_BCCMD_ALL_RANKING_K:
				desc = "广播所有名次";
				break;
			//盲注变化
			case SocketCommand.SVR_BCCMD_BLIND_CHANGE_K:
				desc = "盲注变化";
				break;
			//用户排名
			case SocketCommand.SVR_CMD_USER_RANKING_K:
				desc = "用户排名";
				break;
			//比赛出局
			case SocketCommand.SVR_CMD_USER_OUT_K:
				desc = "比赛出局";
				break;
			//比赛信息
			case SocketCommand.SVR_CMD_MATCH_INFO_K:
				desc = "比赛信息";
				break;
			}
		}
		catch (IOException e) {
			result = null;
		} finally {
			
		}
		
		return result;
	}
		
	/*************************组包函数***********************
	 * 函数头  ：byte[] encode(short code, ByteHashMap hashMap)
	 * 入口参数：
	 * 			1.short code			：命令字
	 * 			2.ByteHashMap hashBody	：组包参数
	 * 
	 * 返回参数：
	 * 			byte[]					：组包数据 
	 *******************************************************/	
	public static byte[] encode(short code, SocketByteHashMap hashMap){
		byte[] result = null;
		
		try{
			switch (code) {
			case SocketCommand.CLI_CMD_GO_PROXY:
				result = encodeClientGoProxy(hashMap);
				break;
			case SocketCommand.CLI_CMD_LOGIN:
				result = encodeClientEnterRoom(hashMap);
				break;
			case SocketCommand.CLI_CMD_SITDOWN:
				result = encodeClientSitdown(hashMap);
				break;
			case SocketCommand.CLI_CMD_REQ_BET:
				result = encodeClientOperation(hashMap);
				break;
			case SocketCommand.CLI_CMD_REQ_SEND_MSG:
				result = encodeClientChat(hashMap);
				break;
			case SocketCommand.CLI_CMD_REQ_SEND_EMOTION:
				result = encodeClientChatEmotion(hashMap);
				break;
			case SocketCommand.CLI_CMD_SEND_HD:
				result = encodeClientInteractiveProps(hashMap);
				break;
			case SocketCommand.CLI_CMD_ADD_FRIEND:
				result = encodeClientAddFriend(hashMap);
				break;
			case SocketCommand.CLI_CMD_REQ_SEND_CHIPS:
				result = encodeClientSendChips(hashMap);
				break;
			}	
		}
		catch (IOException e) {
			
			result = null;
		}
		
		return result;
	}
	
	public final class Enum_Client_GoProxy{
		public final static byte ip = 1; //string
		public final static byte port = 2;//short
	}
	
	public static byte[] encodeClientGoProxy(SocketByteHashMap hashMap) throws IOException {
		byte[] result = null;
		String temp;
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		SocketOutputStream buffer = new SocketOutputStream(out);
		
		temp = hashMap.getString(Enum_Client_GoProxy.ip);
		buffer.writeInt(temp.getBytes().length + 1);
		buffer.write(temp.getBytes());
		buffer.writeByte((byte)0);
		buffer.writeInt(hashMap.getInt(Enum_Client_GoProxy.port));
		
		result = out.toByteArray();
		
		buffer.close();
		buffer = null;
		out.close();
		out = null;
		
//		System.gc();
		
		return result;
	}	
	
	public final class Enum_Client_EnterRoom{
		public final static byte tid = 1; //int
		public final static byte uid = 2;//int
		public final static byte mtkey = 3;//string
		public final static byte imgUrl = 4;//string
		public final static byte giftId = 5;//int
	}
	
	public static byte[] encodeClientEnterRoom(SocketByteHashMap hashMap) throws IOException{
		byte[] result = null;
		String temp;
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		SocketOutputStream buffer = new SocketOutputStream(out);
		
		buffer.writeInt(hashMap.getInt(Enum_Client_EnterRoom.tid));
		buffer.writeInt(hashMap.getInt(Enum_Client_EnterRoom.uid));
		temp = hashMap.getString(Enum_Client_EnterRoom.mtkey);
		buffer.writeInt(temp.getBytes().length + 1);
		buffer.write(temp.getBytes());
		buffer.writeByte((byte)0);
		temp = hashMap.getString(Enum_Client_EnterRoom.imgUrl);
		buffer.writeInt(temp.getBytes().length + 1);
		buffer.write(temp.getBytes());
		buffer.writeByte((byte)0);
		buffer.writeInt(hashMap.getInt(Enum_Client_EnterRoom.giftId));
		
		result = out.toByteArray();
		
		buffer.close();
		buffer = null;
		out.close();
		out = null;
		
		return result;
	}
	
	public final class Enum_Client_Sitdown{
		public static final byte seatId = 1;//byte
		public static final byte buyinChips = 2;//long
	}
	
	public static byte[] encodeClientSitdown(SocketByteHashMap hashMap) throws IOException{
		byte[] result = null;
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		SocketOutputStream buffer = new SocketOutputStream(out);
		
		buffer.writeByte(hashMap.getByte(Enum_Client_Sitdown.seatId));
		buffer.writeLong(hashMap.getLong(Enum_Client_Sitdown.buyinChips));
		
		result = out.toByteArray();
		
		buffer.close();
		buffer = null;
		out.close();
		out = null;
		
//		System.gc();
		
		return result;
	}
	
	public static final class Enum_Client_Operation{
		public static final byte operationType = 1;//byte
		public static final byte betMoney      = 2;//long
	}
	
	public static byte[] encodeClientOperation(SocketByteHashMap hashMap) throws IOException{
		byte[] result = null;
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		SocketOutputStream buffer = new SocketOutputStream(out);
		
		buffer.writeByte(hashMap.getByte(Enum_Client_Operation.operationType));
		buffer.writeLong(hashMap.getLong(Enum_Client_Operation.betMoney));
		
		result = out.toByteArray();
		
		buffer.close();
		buffer = null;
		out.close();
		out = null;
		
//		System.gc();
		
		return result;
	}
	
	public static final class Enum_Client_Chat{
		public static final byte message = 1;//string
	}
	
	public static byte[] encodeClientChat(SocketByteHashMap hashMap) throws IOException{
		byte[] result = null;
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		SocketOutputStream buffer = new SocketOutputStream(out);
		
		String temp = "";
		temp = hashMap.getString(Enum_Client_Chat.message);
		buffer.writeInt(temp.getBytes().length + 1);
		buffer.write(temp.getBytes());
		buffer.writeByte((byte) 0);
		
		result = out.toByteArray();
		
		buffer.close();
		buffer = null;
		out.close();
		out = null;
		
		return result;
	}
	
	public static final class Enum_Client_ChatEmotion{
		public static final byte emotionId   = 1;//int
		public static final byte emotionType = 2;//int
	}
	
	public static byte[] encodeClientChatEmotion(SocketByteHashMap hashMap) throws IOException{
		byte[] result = null;
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		SocketOutputStream buffer = new SocketOutputStream(out);

		buffer.writeInt(hashMap.getInt(Enum_Client_ChatEmotion.emotionType));
		buffer.writeInt(hashMap.getInt(Enum_Client_ChatEmotion.emotionId));
		
		result = out.toByteArray();
		
		buffer.close();
		buffer = null;
		out.close();
		out = null;
		
		return result;
	}
	
	public static final class Enum_Client_InteractiveProps{
		public static final byte senderSeatId   = 1;//byte
		public static final byte InterPropsId = 2;//int
		public static final byte receiverSeatId = 3;//byte
	}
	
	public static byte[] encodeClientInteractiveProps(SocketByteHashMap hashMap) throws IOException{
		byte[] result = null;
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		SocketOutputStream buffer = new SocketOutputStream(out);
		
		buffer.writeByte(hashMap.getByte(Enum_Client_InteractiveProps.senderSeatId));
		buffer.writeInt(hashMap.getInt(Enum_Client_InteractiveProps.InterPropsId));
		buffer.writeByte(hashMap.getByte(Enum_Client_InteractiveProps.receiverSeatId));
		
		result = out.toByteArray();
		
		buffer.close();
		buffer = null;
		out.close();
		out = null;
		
		return result;
	}
	
	public static final class Enum_Client_AddFriend{
		public static final byte senderSeatId = 1;//byte
		public static final byte receiverSeatId = 2;//byte
	}
	
	public static byte[] encodeClientAddFriend(SocketByteHashMap hashMap) throws IOException{
		byte[] result = null;
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		SocketOutputStream buffer = new SocketOutputStream(out);
		
		buffer.writeByte(hashMap.getByte(Enum_Client_AddFriend.senderSeatId));
		buffer.writeByte(hashMap.getByte(Enum_Client_AddFriend.receiverSeatId));
		
		result = out.toByteArray();
		
		buffer.close();
		buffer = null;
		out.close();
		out = null;
		
		return result;
	}
	
	public static final class Enum_Client_SendChips{
		public static final byte senderSeatId = 1;//byte
		public static final byte sendChips    = 2;//long
		public static final byte receiverSeatId =3;//byte
	}
	
	public static byte[] encodeClientSendChips(SocketByteHashMap hashMap) throws IOException{
		byte[] result = null;
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		SocketOutputStream buffer = new SocketOutputStream(out);
		
		buffer.writeByte(hashMap.getByte(Enum_Client_SendChips.senderSeatId));
		buffer.writeLong(hashMap.getLong(Enum_Client_SendChips.sendChips));
		buffer.writeByte(hashMap.getByte(Enum_Client_SendChips.receiverSeatId));
		
		result = out.toByteArray();
		
		buffer.close();
		buffer = null;
		out.close();
		out = null;
		
		return result;
	}
	
	public final static class Enum_Server_Entry_Room_Success{
		//房间信息
		public static final byte smallBlind     = 1;	//小盲注long
		public static final byte minBuyIn       = 2;	//最小买入long
		public static final byte maxBuyIn       = 3;	//最大买入long
		public static final byte tableName      = 4;	//桌子名字string
		public static final byte roomType       = 5;	//房间场别int
		public static final byte tableLevel     = 6;	//房间级别int 
		public static final byte userChips      = 7;	//用户钱数long
		public static final byte betInExpire    = 8;	//下注最大时间int
		public static final byte gameStatus     = 9;	//游戏状态byte
		public static final byte maxSeatCount   = 10;	//最大座位数量byte
		public static final byte roundCount     = 11;	//游戏局数int
		public static final byte dealerSeatId   = 12;	//庄家座位byte
		
		//奖池信息
		public static final byte chipsPotsCount = 13;	//奖池数量byte
		public static final byte chipsPotsValue = 14;   //奖池的值long
		public static final byte chipsPots      = 15;   //奖池数组
		
		//桌子上公共牌信息，在flop和river阶段之间才会有
		//gameStatus介于2（FLOP_ROUND）与4（RIVER_ROUND）之间
		public static final byte publicCards   = 16;   //公共牌short
		public static final byte publicCardsValue   = 40;   
		
		//目前正在下注的座位
		public static final byte betInSeatId   = 17;   //byte
		public static final byte callNeedChips = 18;   //跟注需要钱数long
		public static final byte minRaiseChips = 19;   //加注最小钱数long
		public static final byte maxRaiseChips = 20;   //加注最大钱数long
		
		//玩家信息
		public static final byte playerCount     = 21;   //玩家数量(坐下)byte
		public static final byte playerList      = 22;   //玩家列表
		public static final byte seatId          = 23;   //座位id byte
		public static final byte uid             = 24;   //用户id int 
		public static final byte totalChips      = 25;	 //用户钱数 long
		public static final byte exp             = 26;	 //用户经验 int
		public static final byte vip             = 27;	 //VIP标识 byte
		public static final byte name            = 28;	//用户名 string
		public static final byte gender          = 29;	//性别 string 
		public static final byte photoUrl        = 30;	//用户图片url string
		public static final byte winRound        = 31;	//用户赢盘数 int 
		public static final byte loseRound       = 32;	//用户输盘数 int 
		public static final byte currentPlace    = 33;	//用户所在地 string
		public static final byte homeTown        = 34;	//用户家乡 string
		public static final byte giftId          = 35;	//用户默认道具 int 
		public static final byte seatChips       = 36;	//座位的钱数 long
		public static final byte betInChips      = 37;	//座位的总下注数 long 
		public static final byte operationStatus = 38;	//当前操作类型 byte
		
		public static final byte handCardFlag    = 39;  //是否有手牌 byte
		public static final byte handCard0       = 41;
		public static final byte handCard1       = 42;
		
		public static final byte tableFlag 		 = 43;		//房间标识int
	}
	
	public static SocketByteHashMap decodeServerEntryRoomSuccess(byte[] buf) throws IOException{
		SocketByteHashMap result = new SocketByteHashMap();
		ByteArrayInputStream in = new ByteArrayInputStream(buf);
		SocketInputStream buffer = new SocketInputStream(in);
		
		//房间信息
		result.putLong(Enum_Server_Entry_Room_Success.smallBlind, buffer.readLong());
		result.putLong(Enum_Server_Entry_Room_Success.minBuyIn, buffer.readLong());
		result.putLong(Enum_Server_Entry_Room_Success.maxBuyIn, buffer.readLong());
		int tableNamelen = buffer.readInt();
		result.putString(Enum_Server_Entry_Room_Success.tableName, buffer.readBuffer(tableNamelen));
		result.putInt(Enum_Server_Entry_Room_Success.roomType, buffer.readInt());
		result.putInt(Enum_Server_Entry_Room_Success.tableLevel, buffer.readInt());
		result.putLong(Enum_Server_Entry_Room_Success.userChips, buffer.readLong());
		result.putInt(Enum_Server_Entry_Room_Success.betInExpire, buffer.readInt());
		result.putByte(Enum_Server_Entry_Room_Success.gameStatus, buffer.readByte());
		result.putByte(Enum_Server_Entry_Room_Success.maxSeatCount, buffer.readByte());
		result.putByte(Enum_Server_Entry_Room_Success.dealerSeatId, buffer.readByte());
		
		
		//奖池信息
		result.putByte(Enum_Server_Entry_Room_Success.chipsPotsCount, buffer.readByte());
		
		ArrayList<SocketByteHashMap> chipPots = new ArrayList<SocketByteHashMap>();
		byte chipsPotsCount = result.getByte(Enum_Server_Entry_Room_Success.chipsPotsCount);
		if(chipsPotsCount > 0){
			for(int i = 0; i < chipsPotsCount; i++){
				SocketByteHashMap object = new SocketByteHashMap();
				object.put(Enum_Server_Entry_Room_Success.chipsPotsValue, buffer.readLong());
				chipPots.add(object);
			}
		}
		result.put(Enum_Server_Entry_Room_Success.chipsPots, chipPots);
		
		//gameStatus介于2（FLOP_ROUND）与4（RIVER_ROUND）之间，所以此处需加1
		byte gameStatus = result.getByte(Enum_Server_Entry_Room_Success.gameStatus);
		ArrayList<SocketByteHashMap> publicCards = new ArrayList<SocketByteHashMap>();
		if(gameStatus >=2 && gameStatus <= 4){
			int len = gameStatus + 1;
			for(int i = 0; i < len; i++){
				SocketByteHashMap object = new SocketByteHashMap();
				object.put(Enum_Server_Entry_Room_Success.publicCardsValue, buffer.readShort());
				publicCards.add(object);
			}
		}
		result.put(Enum_Server_Entry_Room_Success.publicCards, publicCards);
		
		result.putByte(Enum_Server_Entry_Room_Success.betInSeatId, buffer.readByte());
		byte betInSeatId = result.getByte(Enum_Server_Entry_Room_Success.betInSeatId);
		if(-1 != betInSeatId){
			result.putLong(Enum_Server_Entry_Room_Success.callNeedChips, buffer.readLong());
			result.putLong(Enum_Server_Entry_Room_Success.minRaiseChips, buffer.readLong());
			result.putLong(Enum_Server_Entry_Room_Success.maxRaiseChips, buffer.readLong());
		}
		
		//玩家信息
//		result.putByte(Enum_Server_Entry_Room_Success.playerCount, buffer.readByte());
//		int playerCount = result.getByte(Enum_Server_Entry_Room_Success.playerCount);
//		ByteHashMap playerList = new ByteHashMap();
//		if(playerCount > 0){
//			for(int i = 0; i < playerCount; i++){
//				TableUserData tableUserData = new TableUserData();
//				
//				tableUserData.setSeatId(buffer.readByte());
//				tableUserData.setUid(buffer.readInt());
//				tableUserData.setTotalChips(buffer.readLong());
//				tableUserData.setExp(buffer.readInt());
//				tableUserData.setVip(buffer.readByte());
//				int namelen = buffer.readInt();
//				tableUserData.setName(buffer.readBuffer(namelen));
//				int genderlen = buffer.readInt();
//				tableUserData.setGender(buffer.readBuffer(genderlen));
//				int photoUrllen = buffer.readInt();
//				tableUserData.setPhotoUrl(buffer.readBuffer(photoUrllen));
//				tableUserData.setWinRound(buffer.readInt());
//				tableUserData.setLoseRound(buffer.readInt());
//				int currentPlacelen = buffer.readInt();
//				tableUserData.setCurrentPlace(buffer.readBuffer(currentPlacelen));
//				int homeTownlen = buffer.readInt();
//				tableUserData.setHomeTown(buffer.readBuffer(homeTownlen));
//				tableUserData.setGiftId(buffer.readInt());
//				tableUserData.setSeatChips(buffer.readLong());
//				tableUserData.setBetInChips(buffer.readLong());
//				tableUserData.setOperationStatus(buffer.readByte());
//				
//				playerList.put(tableUserData.getSeatId(), tableUserData);
//			}
//		}
//		result.put(Enum_Server_Entry_Room_Success.playerList, playerList);
//		
//		result.putByte(Enum_Server_Entry_Room_Success.handCardFlag, buffer.readByte());
//		int handCardFlag = result.getByte(Enum_Server_Entry_Room_Success.handCardFlag);
//		if(1 == handCardFlag){
//			result.putShort(Enum_Server_Entry_Room_Success.handCard0, buffer.readShort());
//			result.putShort(Enum_Server_Entry_Room_Success.handCard1, buffer.readShort());
//		}
//		
//		/* 2013-1-14 srv升级协议,增加平台标示和房间flag */
////		if(pkg.position < pkg.length)
////		{
////			/* 9个位置的平台标示  */	
////			var platFlags:Array = new Array(9);
////			for(var i:int = 0; i < 9; ++i)
////			{
////				platFlags[i] = pkg.readInt();
////			}
////			for each(var player:TableUserData in _loginSuccData.playerList)
////			{
////				player.platFlag = platFlags[player.seatId];
////			}
////			_loginSuccData.tableFlag = pkg.readInt();	
////		}
//		
//		LoginSuccData succData = GameManager.getGameModel().getLoginSuccData();
//		if(null == succData){
//			succData = new LoginSuccData();
//			GameManager.getGameModel().setLoginSuccData(succData);
//		}
//		succData.setSuccData(result);
//		
//		buffer.close();
//		buffer = null;
//		in.close();		
//		in = null;
//		
////		System.gc();
//		
		return result;
	}
	
	public final static class Enum_Server_Sitdown{
		public static final byte seatId       = 1;//byte
		public static final byte uid          = 2;//int
		public static final byte name         = 3;//string
		public static final byte gender       = 4;//string
		public static final byte totalChips   = 5;//long
		public static final byte exp          = 6;//int
		public static final byte vip          = 7;//byte
		public static final byte photoUrl     = 8;//string
		public static final byte winRound     = 9;//int
		public static final byte loseRound    = 10;//int
		public static final byte currentPlace = 11;//string
		public static final byte homeTown     = 12;//String
		public static final byte giftId       = 13;//int
		public static final byte seatChips    = 14;//long
	}
	
	public final static class Enum_Server_SitdownFail{
		public static final byte failCode = 1;//short
	}
	
	public final static class Enum_Server_Standup{
		public static final byte seatId    = 1;//byte
		public static final byte userChips = 2;//long
	}
	
	public final static class Enum_Server_Gamestart{
		public static final byte roundCount   = 1;//int当前局数
		public static final byte dealerSeatId = 2;//byte庄家座位
		public static final byte playerCount  = 3;//byte在玩人数
		public static final byte playerList   = 4;
		public static final byte seatId       = 5;//byte
		public static final byte uid          = 6;//int
		public static final byte seatchips    = 7;//long
		public static final byte handCard1    = 8;//short
		public static final byte handCard2    = 9;//short
		public static final byte userChips    = 10;//long
	}
	
	
	public static final class Enum_Server_PublicCards{
		public static final byte flop  = 0;
		public static final byte flop1 = 1;
		public static final byte flop2 = 2;//short
		public static final byte flop3 = 3;
		public static final byte turn  = 4;
		public static final byte river = 5;
	}
	
	public static SocketByteHashMap decodeServerPublicCards(byte[] buf, byte type) throws IOException{
		SocketByteHashMap result = new SocketByteHashMap();
		ByteArrayInputStream in = new ByteArrayInputStream(buf);
		SocketInputStream buffer = new SocketInputStream(in); 
		
		switch(type){
		case Enum_Server_PublicCards.flop:
			result.putShort(Enum_Server_PublicCards.flop1, buffer.readShort());
			result.putShort(Enum_Server_PublicCards.flop2, buffer.readShort());
			result.putShort(Enum_Server_PublicCards.flop3, buffer.readShort());
			break;
		case Enum_Server_PublicCards.turn:
			result.putShort(Enum_Server_PublicCards.turn, buffer.readShort());
			break;
		case Enum_Server_PublicCards.river:
			result.putShort(Enum_Server_PublicCards.river, buffer.readShort());
			break;
		}
		
		buffer.close();
		buffer = null;
		in.close();		
		in = null;
		
//		System.gc();
		
		return result;
	}
	
	public static final class Enum_Server_OperationTurns{
		public static final byte seatId         = 1;//byte
		public static final byte callChips      = 2;//long 
		public static final byte minRaiseChips  = 3;//long
		public static final byte maxRaiseChips  = 4;//long
	}
	
	public static SocketByteHashMap decodeServerOperationTurns(byte[] buf) throws IOException{
		SocketByteHashMap result = new SocketByteHashMap();
		ByteArrayInputStream in = new ByteArrayInputStream(buf);
		SocketInputStream buffer = new SocketInputStream(in); 
		
		result.putByte(Enum_Server_OperationTurns.seatId, buffer.readByte());
		result.putLong(Enum_Server_OperationTurns.callChips, buffer.readLong());
		result.putLong(Enum_Server_OperationTurns.minRaiseChips, buffer.readLong());
		result.putLong(Enum_Server_OperationTurns.maxRaiseChips, buffer.readLong());
		
		buffer.close();
		buffer = null;
		in.close();		
		in = null;
		
//		System.gc();
		
		return result;
	}
	
	public static final class Enum_Server_OperationResult{
		public static final byte seatId        = 1;//byte
		public static final byte operationType = 2;//byte
		public static final byte betInChips    = 3;//long
	}
	
	public static SocketByteHashMap decodeServerOperationResult(byte[] buf) throws IOException{
		SocketByteHashMap result = new SocketByteHashMap();
		ByteArrayInputStream in = new ByteArrayInputStream(buf);
		SocketInputStream buffer = new SocketInputStream(in); 
		
		result.putByte(Enum_Server_OperationResult.seatId, buffer.readByte());
		result.putByte(Enum_Server_OperationResult.operationType, buffer.readByte());
		result.putLong(Enum_Server_OperationResult.betInChips, buffer.readLong());
		
		buffer.close();
		buffer = null;
		in.close();		
		in = null;
		
//		System.gc();
		
		return result;
	}
	
	public static final class Enum_Server_Pots{
		public static final byte potCount = 1;//byte 
		public static final byte potList  = 2;
	}
	
	public static SocketByteHashMap decodeServerOperationPots(byte[] buf) throws IOException{
		SocketByteHashMap result = new SocketByteHashMap();
		ByteArrayInputStream in = new ByteArrayInputStream(buf);
		SocketInputStream buffer = new SocketInputStream(in); 
		
		result.putByte(Enum_Server_Pots.potCount, buffer.readByte());
		byte potCount = result.getByte(Enum_Server_Pots.potCount);
		ArrayList<Long> potList = new ArrayList<Long>();
		for(byte i = 0; i < potCount; i++){
			potList.add(buffer.readLong());
		}
		result.put(Enum_Server_Pots.potList, potList);
		
		buffer.close();
		buffer = null;
		in.close();		
		in = null;
		
//		System.gc();
		
		return result;
	}
	
	public static final class Enum_Server_GameOverNormal{
		public static final byte expList     = 1;//加减的经验值
		public static final byte chipsList   = 2;//座位钱数
		public static final byte playerCount = 3;//在玩人数
		public static final byte playerList  = 4;//在玩用户的信息
		public static final byte potsCount   = 5;//台面上奖池个数
		public static final byte potsList    = 6;//详细的分钱奖池信息
	}
	
	
	public static final class Enum_Server_Chat{
		public static final byte seatId  = 1;//byte
		public static final byte name    = 2;//string
		public static final byte message = 3;//string
	}
	
	public static SocketByteHashMap decodeServerChat(byte[] buf) throws IOException{ 
		SocketByteHashMap result = new SocketByteHashMap();
		ByteArrayInputStream in = new ByteArrayInputStream(buf);
		SocketInputStream buffer = new SocketInputStream(in);
		
		result.putByte(Enum_Server_Chat.seatId, buffer.readByte());
		int nameLen = buffer.readInt();
		result.putString(Enum_Server_Chat.name, buffer.readBuffer(nameLen));
		int msgLen = buffer.readInt();
		result.putString(Enum_Server_Chat.message, buffer.readBuffer(msgLen));
		
		buffer.close();
		buffer = null;
		in.close();
		in = null;
		
		return result;
	}
	
	public static final class Enum_Server_ChatEmotion{
		public static final byte seatId         = 1;//byte
		public static final byte expressionType = 2;//int
		public static final byte expressionId   = 3;//int
		public static final byte minusMoney     = 4;//long
		public static final byte totalMoney     = 5;//long
	}
	
	public static SocketByteHashMap decodeServerChatEmotion(byte[] buf) throws IOException{ 
		SocketByteHashMap result = new SocketByteHashMap();
		ByteArrayInputStream in = new ByteArrayInputStream(buf);
		SocketInputStream buffer = new SocketInputStream(in);
		
		result.putByte(Enum_Server_ChatEmotion.seatId, buffer.readByte());
		result.putInt(Enum_Server_ChatEmotion.expressionType, buffer.readInt());
		result.putInt(Enum_Server_ChatEmotion.expressionId, buffer.readInt());
		result.putLong(Enum_Server_ChatEmotion.minusMoney, buffer.readLong());
		result.putLong(Enum_Server_ChatEmotion.totalMoney, buffer.readLong());
		
		buffer.close();
		buffer = null;
		in.close();
		in = null;
//		if (_emotionData.expressionId == BIG_EMOTION_ID)
//		{
//			FrameworkGlobal.context.dispatchEventWith(CommandEventNames.ROOM_SOCKET_CONNECTED_DETERMINED);
//		}
		return result;
	}
	
	public static final class Enum_Server_InteractiveProps{
		public static final byte senderSeatId         = 1;//byte
		public static final byte InterPropsId         = 2;//int
		public static final byte receiverSeatId       = 3;//byte
		public static final byte receiverUid          = 4;//int
	}
	
	public static SocketByteHashMap decodeServerInteractiveProps(byte[] buf) throws IOException{ 
		SocketByteHashMap result = new SocketByteHashMap();
		ByteArrayInputStream in = new ByteArrayInputStream(buf);
		SocketInputStream buffer = new SocketInputStream(in);
		
		result.putByte(Enum_Server_InteractiveProps.senderSeatId, buffer.readByte());
		result.putInt(Enum_Server_InteractiveProps.InterPropsId, buffer.readInt());
		result.putByte(Enum_Server_InteractiveProps.receiverSeatId, buffer.readByte());
		result.putInt(Enum_Server_InteractiveProps.receiverUid, buffer.readInt());
		
		buffer.close();
		buffer = null;
		in.close();
		in = null;
		
//		if (Model.instance.getData(ModelKeys.USER_SELF_SEAT_ID) != _hddjData.sendSeatId)
//		{
//			Model.instance.setData(ModelKeys.ROOM_HDDJ_DATA, _hddjData);
//		}
		
		return result;
	}
	
	public static final class Enum_Server_AddFriend{
		public static final byte senderSeatId = 1;//byte
		public static final byte receiverSeatId = 2;//byte
	}
	
	public static SocketByteHashMap decodeServerAddFriend(byte[] buf) throws IOException{
		SocketByteHashMap result = new SocketByteHashMap();
		ByteArrayInputStream in = new ByteArrayInputStream(buf);
		SocketInputStream buffer = new SocketInputStream(in);
		
		result.putByte(Enum_Server_AddFriend.senderSeatId, buffer.readByte());
		result.putByte(Enum_Server_AddFriend.receiverSeatId, buffer.readByte());
		
		buffer.close();
		buffer = null;
		in.close();
		in = null;
		
		return result;
	}
	
	public static final class Enum_Server_SendChips{
		public static final byte senderSeatId = 1;//byte
		public static final byte sendChips    = 2;//long
		public static final byte receiverSeatId = 3;//byte
	}
	public static final class Enum_Server_SendChips_Fail{
		public static final int LESS_THAN_SMALLBLIND    = 0X9401; //钱数不够，不能赠送
		public static final int SEND_TOO_MANY_TIMES     = 0X9402; //赠送太频繁
		public static final int LARGE_THAN_ONE_THOUSAND = 0X9403; //最多赠送1000
		
		public static final byte sendChips = 1;//short
	}
	
	public static class Enum_Server_User_Crash {
		public static final byte times = 1;//short
		public static final byte subsidizeChips    = 2;//long
	}
	
}

